#include <stdio.h>
#include<stdbool.h>
#include"student.h"

char studentid[10];

//显示大类
void show_book_mold(void)
{
	system("clear");
	puts(" 青山湖畔图书系统");
	puts("*****欢迎使用*****");
	puts("1、政治");
	puts("2、法律");
	puts("3、经济");
	puts("4、自然科学");
	puts("5、计算机");
	puts("6、工业与自动化");
	puts("7、小说");
 	puts("8、文学");
	puts("0、退出");
}

//馆藏分类
void book_mold_query(void)
{
	char cmd;
	for(;;)
	{
		show_book_mold();
   		cmd= get_cmd('0','8');
		int cnt=file_num(BOOK_MODE_PATH,category[cmd-'0']);
		char NAME_path[255];
		char isbn[cnt][20];
		switch(cmd)
		{
			case '1'...'8':
    			sprintf(NAME_path,"%s%s",BOOK_MODE_PATH,category[cmd-'0']);
				printf("%s \n",category[cmd-'0']);
 				FILE* rfp = fopen(NAME_path,"r");
    				if(NULL == rfp)
				{
        				show_msg("该类型图书暂无藏书!",1);
					break;
				}
				fscanf(rfp,"%d",&cnt);	
				for(int i=0;i<cnt;i++)
				{
					Book_TYPE data;
					fscanf(rfp,"%s",isbn[i]);
					if(!read_book_isbn(isbn[i],&data))
						break;
					printf("%s %s %s %s\n %c区域 %c书架 第%c层 在馆数量:%u\n",
						data.name,
						data.isbn,
						data.press,
						data.author,
						data.position[0],
						data.position[1],
						data.position[2],
						data.stock_num);
				}
    				fclose(rfp);
 				anykey_continue(); break;
			case '0':return;
		}
	}
}

//搜索图书
void book_search(void)
{
	char name[33];
	char isbn[20];
	for(;;)
	{
		system("clear");
		puts(" 青山湖畔图书系统");
		puts("*****欢迎使用*****");
		puts("1、书本编号查询");
		puts("2、书本名称查询");
		puts("0、退出");
		switch(get_cmd('0','2'))
		{
			case '1':
				printf("请输入书号");
				get_str(isbn,sizeof(isbn));
				Book_TYPE data;
				if(!read_book_isbn(isbn,&data))
					break;
				printf("%s %s %s %s\n %c区域 %c书架 第%c层 在馆数量:%u\n",
					data.name,data.isbn,data.press,data.author,
					data.position[0],data.position[1],
					data.position[2],data.stock_num);
				if(0 == data.stock_num)
				{				
					printf("是否需要预约(y/n)?\n");
					if(yes_or_no())
					{
						add_record(BOOK_ORDER_PATH,data.name,studentid);
						add_record(STUDENT_ORDER_PATH,studentid,isbn);
						show_msg("预约成功!",1);
                    }
				}
				anykey_continue();
				break;
			case '2':
				printf("请输入书名:");
				get_str(name,sizeof(name));
				read_book_name(name);break;
			case '0':return ;
		}
	}
}

//图书查找函数
void book_query(void)
{
	for(;;)
	{
		system("clear");
		puts(" 青山湖畔图书系统");
		puts("*****欢迎使用*****");
		puts("1、馆藏目录");
		puts("2、图书搜索");
		puts("0、退出");
		switch(get_cmd('0','2'))
		{
			case '1':book_mold_query();break;
			case '2':book_search();break;
			case '0':return;
		}
	}	
}

//借书记录
void read_student_borrow(void)
{
	
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",STUDENT_BORROW_PATH,studentid);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
	{
		show_msg("暂无借书记录!",1);
        return ;
	}
	int num;
	fscanf(rfp,"%d",&num);
	for(int i=0;i<num;i++)
	{
		Book_TYPE val;
		Student_Book data;
		fscanf(rfp,"%s %s %d %d %d",data.name,data.isbn,
							&data.borrow.year,
							&data.borrow.mon,
							&data.borrow.day);
		read_book_isbn(data.isbn,&val);
		Time t = get_time();
		int sum = -OVER_TIME;
		sum+=(t.year-data.borrow.year)*12;
		sum+=(t.mon-data.borrow.mon);
		if(t.day > data.borrow.day)
			sum++;
		sum = sum<0 ? 0:sum;
		double money;
		money = val.price*OVER_PRICE*sum;
		
		printf("%s %s 借书时间：%d：%d：%d \n",data.name,data.isbn,
							data.borrow.year+1900,
							data.borrow.mon+1,
							data.borrow.day);
		if(0 < sum)
				printf("您已逾期%d月,请尽快归还，现需要缴费:%lf元",sum,money);
		
	 }
	fclose(rfp);  
	anykey_continue(); 
}

//还书记录
void read_student_return(void)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",STUDENT_RETURN_PATH,studentid);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
	{
		show_msg("暂无还书记录!",1);
        return ;
	}
	int num;
	fscanf(rfp,"%d",&num);
	for(int i=0;i<num;i++)
	{
		Student_Book data;
		fscanf(rfp,"%s %s %d %d %d %d %d %d",data.name,data.isbn,
			&data.borrow.year,
			&data.borrow.mon,
			&data.borrow.day,
			&data.back.year,
			&data.back.mon,
			&data.back.day);
		printf("%s %s 借书时间：%d：%d：%d    还书时间：%d：%d：%d\n",data.name,data.isbn,
			data.borrow.year+1900,
			data.borrow.mon+1,
			data.borrow.day,
			data.back.year+1900,
			data.back.mon+1,
			data.back.day);
    }
	fclose(rfp);   
	anykey_continue();
}

//查看预约记录
void read_student_order(void)
{
	int cnt=file_num(STUDENT_ORDER_PATH,studentid);
	char NAME_path[255];
	char isbn[cnt][20];
    sprintf(NAME_path,"%s%s",STUDENT_ORDER_PATH,studentid);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
	{
        show_msg("暂无预约记录!",1);
		return ;
	}
	fscanf(rfp,"%d",&cnt);	
	for(int i=0;i<cnt;i++)
	{
		Book_TYPE data;
		fscanf(rfp,"%s",isbn[i]);
		read_book_isbn(isbn[i],&data);
		printf("%s %s %s %s",data.name,data.isbn,data.press,data.author);
	}
    	fclose(rfp);  
	anykey_continue();
}
//显示
void show_studnet(void)
{
	system("clear");
	puts(" 青山湖畔图书系统");
	puts("*****欢迎使用*****");
	puts("1、图书查询");
	puts("2、借书记录");
	puts("3、还书记录");
	puts("4、预约记录");
	puts("5、退出账号");
}

//主函数
void student_interface(void)
{
	printf("请输入学号:");
	scanf("%s",studentid);
	if(!student_login(studentid))
		return;	
	for(;;)
	{
		show_studnet();
		switch(get_cmd('1','5'))
        	{
			case '1':book_query(); break;     
			case '2':read_student_borrow();break;          
			case '3':read_student_return();break;
			case '4':read_student_order();break;
			case '5':return ;
        	}
	}
}
